<template>
  <!-- 产品推荐配置 -->
  <div class="product-container">
    <vab-query-form>
      <vab-query-form-left-panel>
        <el-button icon="el-icon-plus" type="primary" @click="handleAdd">新增产品</el-button>
        <!-- <el-button icon="el-icon-delete" type="danger" @click="handleDelete">删除</el-button> -->
      </vab-query-form-left-panel>
    </vab-query-form>

    <el-table
      :key="itemKey"
      ref="tableSort"
      v-loading="listLoading"
      :data="list"
      :element-loading-text="elementLoadingText"
      :height="height"
      @selection-change="setSelectRows"
    >
      <!-- <el-table-column show-overflow-tooltip type="selection" width="55" /> -->
      <el-table-column label="序号" show-overflow-tooltip width="95">
        <template #default="scope">
          {{ scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column label="产品名称" prop="product.name" show-overflow-tooltip />
      <el-table-column label="微信产品id" prop="product.id" show-overflow-tooltip />
      <el-table-column label="淘宝产品id" prop="product.tmallId" show-overflow-tooltip />
      <el-table-column label="抖音产品id" prop="product.dyId" show-overflow-tooltip />
      <el-table-column label="操作" show-overflow-tooltip width="180px">
        <template #default="scope">
          <el-button type="primary" @click="handleEdit(scope.row)">编辑</el-button>
          <el-button type="danger" @click="handleDelete(scope.row, scope.$index)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <table-edit ref="edit" @fetch-data="fetchData" />
  </div>
</template>

<script>
  import { doDelete, doEdit } from '@/api/configration'
  import TableEdit from './components/TableEdit'

  // import { getDeletePrizeConfigProps } from '../utils.js'
  export default {
    name: 'ProductTable',
    components: {
      TableEdit,
    },
    filters: {
      statusFilter(status) {
        const statusMap = {
          published: 'success',
          draft: 'gray',
          deleted: 'danger',
        }
        return statusMap[status]
      },
    },
    data() {
      return {
        imgShow: true,
        apiData: [],
        list: [],
        imageList: [],
        listLoading: true,
        itemKey: false,
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        background: true,
        selectRows: '',
        elementLoadingText: '正在加载...',
        queryForm: {
          pageNo: 1,
          pageSize: 20,
          title: '',
        },
      }
    },
    computed: {
      height() {
        return this.$baseTableHeight()
      },
    },
    created() {
      console.log('created...')
      this.fetchData()
    },
    beforeDestroy() {},
    mounted() {},
    methods: {
      setSelectRows(val) {
        this.selectRows = val
      },
      handleAdd() {
        this.$refs['edit'].showEdit(this.apiData)
      },
      handleEdit(row) {
        this.$refs['edit'].showEdit(this.apiData, row)
      },
      calcList() {},
      handleDelete(row, rowIndex) {
        console.log(row, 'handleDelete...', rowIndex)
        if (row && typeof rowIndex === 'number') {
          this.$baseConfirm('你确定要删除当前项吗', null, async () => {
            // const p = getDeletePrizeConfigProps(row, this.apiData)
            this.list.splice(rowIndex, 1)
            const props = {
              configValue: this.list,
              configItem: 'Product',
            }
            let { data, error } = await doEdit(props).catch((err) => {
              this.$baseMessage('接口报错', 'error')
            })
            if (!error) {
              this.$baseMessage('操作成功', 'success')
              this.fetchData()
            }
          })
        } else {
          // if (this.selectRows.length > 0) {
          //   const ids = this.selectRows.map((item) => item.id).join()
          //   this.$baseConfirm('你确定要删除选中项吗', null, async () => {
          //     const { msg } = await doDelete({ ids: ids })
          //     this.$baseMessage(msg, 'success')
          //     this.fetchData()
          //   })
          // } else {
          this.$baseMessage('未选中任何行', 'error')
          return false
          // }
        }
      },
      handleSizeChange(val) {
        console.log('handleSizeChange...', this.itemKey)

        this.queryForm.pageSize = val
        this.fetchData()
      },
      handleCurrentChange(val) {
        console.log('handleCurrentChange...', this.itemKey)

        this.queryForm.pageNo = val
        this.fetchData()
      },
      handleQuery() {
        console.log('handleQuery...', this.itemKey)

        this.queryForm.pageNo = 1
        this.fetchData()
      },
      async fetchData() {
        console.log('fetchData...', this.itemKey)
        this.itemKey = !this.itemKey
        this.apiData = {}
        this.list = []
        this.listLoading = true
        this.$store
          .dispatch('config/getProductList')
          .then((data) => {
            console.log('config/getProductList', data)
            this.apiData = data

            this.list = data
            if (data && Array.isArray(data)) {
              data.map((item, index) => {
                if (item && typeof item === 'object') {
                  let { product: { id, tmallId, dyId } = {} } = item
                  this.list[index].product.ids = id || tmallId || dyId
                }
              })
              // console.error(data, 'errorerror', this.list.product)
              // try {
              //   this.list.product.ids = { id, tmallId, dyId }
              // } catch (error) {
              //   console.error('errorerror', error)
              // }
            }

            this.total = this.list.length
            setTimeout(() => {
              this.listLoading = false
            }, 500)
          })
          .catch()
      },
    },
  }
</script>
